package fieldmaps;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;

import javax.vecmath.Point2d;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;

import fieldmaps.database.DatabaseGenericMap;
import fieldmaps.database.DatabaseRasterMap;
import fieldmaps.files.LoadRasterMapFromFile;

/**
 * FieldMaps!
 *
 */
public class Run2 {
    public static void main( String[] args ) throws SQLException, IOException {

    	LoadRasterMapFromFile mapFile = new LoadRasterMapFromFile();
    	DatabaseGenericMap dbMap = mapFile.createMapFromFile("./dataRaster/prec_10m_bil/prec0", "Prec0");

//    	DatabaseRasterMap dbMap = DatabaseRasterMap.loadMap("Prec0");
    	List<Polygon> cell = dbMap.getCell(new Coordinate(0,0));
    	System.out.println(cell.get(0));

    	System.out.println("Neighbors:");
    	List<Polygon> neighbors = dbMap.getCellNeighbors(cell.get(0));
    	
    	for (Polygon neighbor: neighbors) {
    		System.out.println(neighbor);
    	}
//     	writePoints(dbMap);
//     	testPolygons(dbMap);
    }
    
    private static void writePoints(DatabaseGenericMap dbMap) throws IOException {

    	Polygon poligono = null;
    	BufferedWriter bw = new BufferedWriter(new FileWriter(new File("../points.csv")));
    	dbMap.startCellIteration();

    	try {
			while((poligono = dbMap.getNextCell()) != null) {
				Coordinate[] coords = poligono.getCoordinates();
				for (Coordinate coord: coords) {
//					String s = coord.x + "," + coord.y + ",,"; 
//					bw.write(s);
					String s = coord.x + "," + coord.y; 
					bw.write(s);
					bw.write("\n");
				}
//				bw.write("\n");
			}

		} catch (NoSuchElementException e) {
			System.out.println("No more elements!");
		}
		bw.close();
    }
    
    
    private static void testPolygons(DatabaseGenericMap dbMap) throws IOException {

    	Polygon poligono = null;
    	dbMap.startCellIteration();
    	HashSet<Coordinate> var = new HashSet<Coordinate>();
    	int count = 0;

    	try {
			while((poligono = dbMap.getNextCell()) != null) {
				Coordinate[] coords = poligono.getCoordinates();
				for (Coordinate coord: coords) {
					var.add(coord);
					count = count+1;
				}
			}

		} catch (NoSuchElementException e) {
			System.out.println("No more elements!");
		}

		System.out.println(count);
		System.out.println(var.size());

    }

    
//    private void showMinMaxValues() {
//    	
//    	DatabaseGenericMap dbMap = new DatabaseGenericMap();
//    	dbMap.startCellIteration();
//    	Polygon poligono = null;
//    	
//    	double xmin = Double.MAX_VALUE;
//    	double xmax = Double.MIN_VALUE;
//    	
//    	double ymin = Double.MAX_VALUE;
//    	double ymax = Double.MIN_VALUE;
//    	
//    	try {
//			while((poligono = dbMap.getNextCell()) != null) {
//				Point punto = poligono.getCentroid();
//				double x = punto.getX();
//				double y = punto.getY();
//				if(x<xmin)
//					xmin = x;
//				else if(x>xmax)
//					xmax = x;
//				if(y<ymin)
//					ymin = y;
//				else if (y>ymax)
//					ymax = y;
//			}
//		} catch (NoSuchElementException e) {
//	    	System.out.println(xmin);
//	    	System.out.println(xmax);
//	    	System.out.println(ymin);
//	    	System.out.println(ymax);
//		}
//    }
    
    private static Polygon createPolygon (Point2d[] points) {
    	List<Coordinate> coordinates = new ArrayList<Coordinate>();
    	for (Point2d point: points) {
			Coordinate coordinate = new Coordinate();
			coordinate.x = Double.valueOf(point.x);
			coordinate.y = Double.valueOf(point.y);
			coordinates.add(coordinate);
		}
		LinearRing linearRing = new LinearRing(new CoordinateArraySequence(coordinates.toArray(new Coordinate[0])), new GeometryFactory());
		return new Polygon(linearRing, null, new GeometryFactory());
    }
}
